package com.vtech.spring.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 发送邮件
 * https://mrbird.cc/Spring-Boot-Email.html
 * @author houzhiwei
 * @date 2018/6/10 9:57
 */
@RestController("/email/")
public class EmailController {
    @Autowired
    private JavaMailSender jms;

    @Value("spring.email.username")
    private String from;

    @RequestMapping("sendEmail")
    public String sendSimpleEmail() {
        try {
//            简单文字版邮件
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo("888888@qq.com");
            message.setSubject("一封简单的邮件");
            message.setText("使用Spring Boot发送简单邮件。");
            jms.send(message);
            return "发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
